package cn.com.sina.finance.todolist.net;

import android.util.Log;

import androidx.annotation.NonNull;

import java.io.IOException;

import cn.com.sina.finance.todolist.util.SpUtil;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 登录拦截器，统一给网络请求Header中添加Cookie
 *
 * @author yuebo
 * @date 2024/10/16
 */
public class LoginInterceptor implements Interceptor {

    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request request = chain.request();
        Log.d("todo", "LoginInterceptor.intercept() url=" + request.url());
        Request newRequest = request.newBuilder()
                .addHeader("Cookie", "JSESSIONID=" + getCookie())
                .build();
        return chain.proceed(newRequest);
    }

    @NonNull
    public String getCookie() {
        String cookie = SpUtil.getString(SpUtil.COOKIE, null);
        if (cookie == null) {
            return "";
        }
        return cookie;
    }
}
